<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db; 
use think\Request;
use think\Cache;

class type extends Common
{
	/**
	 * [type 课程分类展示]
	 * @return [type] [description]
	 */
	public function index(){
		$request = Request::instance();
		$param = $request->param();
		$where = $pageParam['query'] = [];
		if(input('post.') || $param['nav_id']){
			$data_param = $param;
			if(!$data_param){
				$data_param['nav_id'] = $param['nav_id'];
				$data_param['status'] = 'sel';
			}else{
				$type_ids = $data_param['type_id'];
				$type_sorts = $data_param['type_sort'];
				$keywords = $data_param['keywords'];
			}
			$status = $data_param['status'];
			if($status == 'del'){
				foreach ($type_ids as $key => $id) {
					$specifics = DB::table('specific')->where('type_id',$id)->find();
				}
				if($specifics){
					$this->error('该分类下有章节，不能被删除');die;
				}
				$type_id = implode(',', $type_ids);
				$del = Db::table('type')->where('type_id','in',$type_id)->delete($type_id);
				if($del){
					$this->success('删除成功','index','',1);
				}
			}else if($status == 'sort'){
				foreach ($type_ids as $key => $id) {
					$up = Db::table('type')->where('type_id',$id)->update(['type_sort'=>$type_sorts[$id]]);
				}
				if($up){
					$this->success('排序成功','index','',1);
				}
			}else if($status == 'move'){
				foreach ($type_ids as $key => $id) {
					Db::table('type')->where('type_id',$id)->update(['nav_id'=>$param['nav_id']]);
					Db::table('specific')->where('type_id',$id)->update(['nav_id'=>$param['nav_id']]);
					Db::table('course')->where('type_id',$id)->update(['nav_id'=>$param['nav_id']]);
					Db::table('training')->where('type_id',$id)->update(['nav_id'=>$param['nav_id']]);
				}
				$this->success('移动成功','index','',1);
			}else{
				$nav_id = $param['nav_id'];
				if($param['nav_id']){
					$where['n.nav_id'] = $param['nav_id'];
				}
				if($keywords){
					$where['type_name'] = ['like',"%$keywords%"];
				}
				$pageParam['query'] = $data_param;
			}
		}
		$data = Db::table('type')->join('nav n','type.nav_id = n.nav_id')
					->where($where)->order('type_sort desc,type_id asc')
					->paginate(20,false,$pageParam);
		$nav_data = Db::table('nav')->field('nav_id,nav_name')->order('nav_sort desc')->select();
		return $this->fetch('type',[
			'data' => $data,
			'param' => $param,
			'nav_data' => $nav_data,
		]);
	}

	/**
	 * [typeedit 课程分类修改页面]
	 * @return [type] [description]
	 */
	public function typeedit(){
		$request = Request::instance();
		$type_id = $request->param('type_id');
		$type_data = Db::table('type')->where('type_id',$type_id)->find();
		if(input('post.')){
			$req = input('post.');
			if ($type_id){
				$datas = Db::table('type')->where('type_id',$type_id)->update($req);
			}else{
				$datas = Db::table('type')->insert($req);
			}
			if($type_data['nav_id'] != $req['nav_id']){
				Db::table('specific')->where('type_id',$type_id)->update(['nav_id'=>$req['nav_id']]);
				Db::table('course')->where('type_id',$type_id)->update(['nav_id'=>$req['nav_id']]);
			}
			if($datas){
				$this->success('编辑成功',url('index').'&nav_id='.$req['nav_id'],'',1);
			}else{
				$this->error('编辑失败');
			}
		}else{
			$nav_data = Db::table('nav')->field('nav_id,nav_name')->order('nav_sort desc')->select();
			$this->assign('nav_data',$nav_data);
			$this->assign('type_data',$type_data);
			return $this->fetch('typeedit');
		}
		
	}


	/**
	 * [typedelete 课程分类删除]
	 * @return [type] [description]
	 */
	public function typedelete(){
		$request = Request::instance();
		$type_id = $request->param('type_id');
		$courses = DB::table('specific')->where('type_id',$type_id)->find();
		if($courses){
			$this->error('该分类下有章节，不能被删除');
		}else{
			if(DB::table('type')->where('type_id',$type_id)->delete()){
				$this->success('删除成功','index','',1);
			}else{
				$this->error('删除失败');
			}
		}
	}
}